package org.csu.laboratoryequipment.service.impl;

import org.csu.laboratoryequipment.common.CommonResponse;
import org.csu.laboratoryequipment.entity.User;
import org.csu.laboratoryequipment.persistence.UserMapper;
import org.csu.laboratoryequipment.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.stereotype.Service;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    public CommonResponse<List<User>> getUserList(){
        List<User> userList = userMapper.selectList(null);
        List<Link> linkList = new ArrayList<>();
        //自己的link
        Link link1 = new Link("http://localhost:8088/users", "self");
        linkList.add(link1);
        //页面中可能存在的link
        for (int i = 0; i < userList.size(); i++) {
            String id = userList.get(i).getUserID();
            Link link = new Link("http://localhost:8088/users/"+id, "user");
            linkList.add(link);
        }
        return CommonResponse.createForSuccessMessage("SUCCESS", userList,linkList);
    }

    @Override
    public CommonResponse<User> getUserById(String userid){
        User user = userMapper.selectById(userid);
        List<Link> linkList = new ArrayList<>();
        String name = user.getUserID();
        //自己的link
        Link link1 = new Link("http://localhost:8088/users/"+name, "self");
        linkList.add(link1);
        return CommonResponse.createForSuccessMessage("SUCCESS", user,linkList);
    }

    @Override
    public CommonResponse<List<User>> deleteUserById(String userid){
        userMapper.deleteById(userid);
        List<User> userList = userMapper.selectList(null);
        List<Link> linkList = new ArrayList<>();
        //自己的link
        Link link1 = new Link("http://localhost:8088/users", "self");
        linkList.add(link1);
        //页面中可能存在的link
        for (int i = 0; i < userList.size(); i++) {
            String id = userList.get(i).getUserID();
            Link link = new Link("http://localhost:8088/users/"+id, "user");
            linkList.add(link);
        }
        return CommonResponse.createForSuccessMessage("删除成功", userList,linkList);
    }

    @Override
    public CommonResponse<List<User>> postUser(String userid,String name,String department,int age){
        User user = new User(userid,name,department,age);
        userMapper.insert(user);
        List<User> userList = userMapper.selectList(null);
        List<Link> linkList = new ArrayList<>();
        //自己的link
        Link link1 = new Link("http://localhost:8088/users", "self");
        linkList.add(link1);
        //页面中可能存在的link
        for (int i = 0; i < userList.size(); i++) {
            String id = userList.get(i).getUserID();
            Link link = new Link("http://localhost:8088/users/"+id, "user");
            linkList.add(link);
        }
        return CommonResponse.createForSuccessMessage("添加成功", userList,linkList);
    }

    @Override
    public CommonResponse<List<User>> putUser(String userid,String name,String department,int age){
        User user = new User(userid,name,department,age);
        userMapper.updateById(user);
        List<User> userList = userMapper.selectList(null);
        List<Link> linkList = new ArrayList<>();
        //自己的link
        Link link1 = new Link("http://localhost:8088/users", "self");
        linkList.add(link1);
        //页面中可能存在的link
        for (int i = 0; i < userList.size(); i++) {
            String id = userList.get(i).getUserID();
            Link link = new Link("http://localhost:8088/users/"+id, "user");
            linkList.add(link);
        }
        return CommonResponse.createForSuccessMessage("修改成功", userList,linkList);
    }
}
